package com.transaction.controller;

import com.transaction.domain.LoginUser;
import com.transaction.domain.User;
import com.transaction.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

@Controller
@RequestMapping("/user")
@SessionAttributes("uid")
public class UserController {

    @Autowired
    private UserService userService;

    //用户登录
    @RequestMapping("/login")
    public @ResponseBody
    LoginUser userLogin(@RequestBody User user){
        user=userService.userLogin(user);
        LoginUser loginUser=new LoginUser();
        loginUser.setUser(user);
        if (user==null){
            loginUser.setStatus(false);
        } else {
            loginUser.setStatus(true);
        }
        return loginUser;
    }

    //用户注册
    @RequestMapping("/regist")
    public @ResponseBody
    LoginUser userRegister(@RequestBody User user){
        boolean r=userService.userRegister(user);
        LoginUser loginUser=new LoginUser();
        loginUser.setUser(user);
        if (r==true){
            System.out.println(user);
            loginUser.setStatus(true);
        }else {
            loginUser.setStatus(false);
        }
        return loginUser;
    }

    //修改用户
    @RequestMapping("/update")
    public @ResponseBody
    Boolean userUpdate(@RequestBody User user){
        System.out.println(user);
        boolean r=userService.userUpdate(user);
        return r;
    }


}
